import '@babel/polyfill';
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
import gsap from 'gsap';
import * as dat from 'dat.gui';
import { DoubleSide } from 'three';

//场景
const scene = new THREE.Scene();

//相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 200);
camera.position.set(5, 3, 5);
scene.add(camera);

//渲染器
const renderer = new THREE.WebGL1Renderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

//mesh
const cubeGoemetry=new THREE.BoxGeometry(1,1,1);
const cubeMaterial=new THREE.MeshStandardMaterial({
    color:0xff0000,
});
const cube=new THREE.Mesh(cubeGoemetry,cubeMaterial);
cube.position.set(0,0,0);
scene.add(cube);

const planeGeometry=new THREE.PlaneGeometry(20,20);
const planeMaterial=new THREE.MeshStandardMaterial({color:0x808080});
const plane=new THREE.Mesh(planeGeometry,planeMaterial);
plane.position.set(0,-2,0);
plane.rotateX(-Math.PI/2);
scene.add(plane);

//创建光
const light = new THREE.AmbientLight(0xf2f2f2, 0.3);//环境光，无阴影

const spotLight = new THREE.SpotLight(0xffffff);//聚光灯
spotLight.position.set(7, 7, 7);

const directionalLight = new THREE.DirectionalLight(0xf2f2f2, 1);//平行光，模拟太阳光
directionalLight.position.set(30, 30, 30);

const pointLight = new THREE.PointLight(0xff0000, 0.5);//点光源
//pointLight.position.set(8,8,8);
//scene.add(pointLight);

scene.add(spotLight);

//阴影
renderer.shadowMap.enabled=true;
spotLight.castShadow=true;
plane.receiveShadow=true;
cube.castShadow=true;

//控制
const orbitControls = new OrbitControls(camera, renderer.domElement);
orbitControls.enableDamping = true;

//坐标轴
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);


//帧动画
function animate() {
    orbitControls.update();
    renderer.render(scene, camera);
    requestAnimationFrame(animate);
};
animate();

//窗口调整侦听
window.addEventListener('resize', () => {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setPixelRatio(window.devicePixelRatio);
});



